﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompanyManagementDLL.MetaData;

namespace CompanyManagementDLL.DLL
{
    public class UserDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();
        #region Fecth Data
        /// <summary>
        /// Return user detail based in user id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserMetaData GetUserByID(int id)
        {
            UserMetaData usermetadta = new UserMetaData();
            try
            {
                usermetadta = db.Users.Where(r => r.UserID == id).Select(u => new UserMetaData
                {
                    UserID = u.UserID,
                    UserName = u.UserName,
                    FullName = u.FullName,
                    Password = u.Password,
                    ConfirmPassword = u.Password,
                    LastLoginDate = u.LastLoginDate,
                    EmailID = u.EmailID,
                    IsActive = u.IsActive.Value,
                    PhoneNumber = u.PhoneNumber,
                    RoleID = u.RoleID,
                    Address = u.Address,
                    CreatedDate = u.CreatedDate,
                    ProfilePicPath = u.ProfilePicPath
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return usermetadta;
        }

        /// <summary>
        /// Return user detail based in user name
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserMetaData GetUserByUsername(string username)
        {
            UserMetaData usermetadta = new UserMetaData();
            try
            {
                usermetadta = db.Users.Where(r => r.UserName == username).Select(u => new UserMetaData
                {
                    UserID = u.UserID,
                    UserName = u.UserName,
                    FullName = u.FullName,
                    Password = u.Password,
                    LastLoginDate = u.LastLoginDate,
                    EmailID = u.EmailID,
                    IsActive = u.IsActive.Value,
                    PhoneNumber = u.PhoneNumber,
                    RoleID = u.RoleID,
                    Address = u.Address,
                    CreatedDate = u.CreatedDate,
                    ProfilePicPath = u.ProfilePicPath
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return usermetadta;
        }

        /// <summary>
        /// Return user detail based in email address
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserMetaData GetUserByEmailAddress(string emailid)
        {
            UserMetaData usermetadta = new UserMetaData();
            try
            {
                usermetadta = db.Users.Where(r => r.EmailID == emailid).Select(u => new UserMetaData
                {
                    UserID = u.UserID,
                    UserName = u.UserName,
                    FullName = u.FullName,
                    Password = u.Password,
                    LastLoginDate = u.LastLoginDate,
                    EmailID = u.EmailID,
                    IsActive = u.IsActive.Value,
                    PhoneNumber = u.PhoneNumber,
                    RoleID = u.RoleID,
                    Address = u.Address,
                    CreatedDate = u.CreatedDate,
                    ProfilePicPath = u.ProfilePicPath
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return usermetadta;
        }

        #endregion

        #region Get List

        /// <summary>
        /// Retrun user list
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<UserMetaData> GetUserList()
        {
            List<UserMetaData> usermetadata = new List<UserMetaData>();
            try
            {
                usermetadata = db.Users.Where(u => u.IsActive == true).Select(u => new UserMetaData
                {
                    UserID = u.UserID,
                    UserName = u.UserName,
                    FullName = u.FullName,
                    Password = u.Password,
                    LastLoginDate = u.LastLoginDate,
                    EmailID = u.EmailID,
                    IsActive = u.IsActive.Value,
                    PhoneNumber = u.PhoneNumber,
                    RoleID = u.RoleID,
                    Address = u.Address,
                    CreatedDate = u.CreatedDate,
                    ProfilePicPath = u.ProfilePicPath
                }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return usermetadata;
        }

        /// <summary>
        /// Get Project assign list
        /// </summary>
        /// <returns></returns>
        public List<UserMetaData> getUserListByProjectID(int projectid)
        {
            List<UserMetaData> list = new List<UserMetaData>();
            try
            {
                list = db.ProjectAssigns.Where(pa => pa.ProjectID == projectid).Select(pa => new UserMetaData
                {
                    UserID = pa.AssignID.Value,
                    UserName = db.Users.Where(u => u.UserID == pa.AssignID).FirstOrDefault().UserName,
                    RoleID = pa.ProjectID,
                    TID = pa.ID
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return list;
        }

        #endregion

        #region DML Opration

        /// <summary>
        /// insert user entity
        /// </summary>
        /// <param name="usermetadata"></param>
        public void InsertUser(UserMetaData usermetadata)
        {
            User tblUser = new User();
            try
            {
                tblUser.UserName = usermetadata.UserName;
                tblUser.UserName = usermetadata.UserName;
                tblUser.FullName = usermetadata.FullName;
                tblUser.Password = usermetadata.Password;
                tblUser.EmailID = usermetadata.EmailID;
                tblUser.IsActive = usermetadata.IsActive;
                tblUser.PhoneNumber = usermetadata.PhoneNumber;
                tblUser.RoleID = usermetadata.RoleID;
                tblUser.Address = usermetadata.Address;
                tblUser.CreatedDate = DateTime.Now;
                db.Users.Add(tblUser);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// update user entity
        /// </summary>
        /// <param name="usermetadata"></param>
        public void UpdateUser(UserMetaData usermetadata)
        {
            User tblUser = new User();
            try
            {
                tblUser = db.Users.Where(r => r.UserID == usermetadata.UserID).FirstOrDefault();
                tblUser.UserName = usermetadata.UserName;
                tblUser.UserName = usermetadata.UserName;
                tblUser.FullName = usermetadata.FullName;
                tblUser.Password = usermetadata.Password;
                tblUser.EmailID = usermetadata.EmailID;
                tblUser.IsActive = usermetadata.IsActive;
                tblUser.PhoneNumber = usermetadata.PhoneNumber;
                tblUser.RoleID = usermetadata.RoleID;
                tblUser.Address = usermetadata.Address;
                tblUser.LastLoginDate = usermetadata.LastLoginDate;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete user entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteUser(int id)
        {
            try
            {
                User tblUser = new User();
                tblUser = db.Users.Where(r => r.UserID == id).FirstOrDefault();
                db.Users.Remove(tblUser);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion

        #region Validate User

        public bool ValidateUser(UserData userdata)
        {
            bool isValid = false;
            try
            {
                var user = db.Users.Where(u => u.UserName == userdata.UserName && u.Password == userdata.Password && u.IsActive == true).FirstOrDefault();
                if (user != null)
                {
                    isValid = true;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return isValid;
        }

        public bool ChangePassword(int userid, string oldpassword, string newpassword)
        {
            bool isChange = false;
            try
            {
                var user = db.Users.Where(u => u.UserID == userid && u.Password == oldpassword).FirstOrDefault();
                if (user != null)
                {
                    user.Password = newpassword;
                    db.SaveChanges();
                    isChange = true;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return isChange;
        }
        #endregion
    }
}
